package net.tngou.iask.action.my;

import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Agerange;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.Department;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Place;
import net.tngou.iask.pojo.Treatment;
import net.tngou.iask.service.DiseaseService;

/**
 * 疾病信息管理
 * @author tngou@tngou.net
 *
 */
public class DiseaseAction extends BaseAction {

	/**
	 * 疾病列表
	 */
	public void list() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		DiseaseService service = new DiseaseService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		root.put("dtype", "disease");
		printFreemarker("my/disease_list.ftl", root);
	}
	
	/**
	 * 添加疾病
	 */
	@SuppressWarnings("unchecked")
	public void add() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		List<?> places = (List<? extends POJO>) service.getList(Place.class);//身体位置
		List<?> departments = (List<? extends POJO>) service.getList(Department.class);//科室
		List<?> treatments = (List<? extends POJO>) service.getList(Treatment.class);//治疗方法段
		List<?> ageranges = (List<? extends POJO>) service.getList(Agerange.class);//年龄阶段
		root.put("places", places);
		root.put("departments", departments);
		root.put("treatments", treatments);
		root.put("ageranges", ageranges);
		root.put("dtype", "disease");
		printFreemarker("my/disease_add.ftl", root);
	}
	
	/**
	 * 更新疾病
	 */
	public void update() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Disease disease=new Disease().get(id);
		if(disease==null){run_500();return;}
		List<? extends POJO> places = new Place().list();//身体位置
		List<? extends POJO> departments = new Department().list();//科室
		List<? extends POJO> treatments = new Treatment().list();//治疗方法段
		List<? extends POJO> ageranges = new Agerange().list();//年龄阶段
		root.put("places", places);
		root.put("departments", departments);
		root.put("treatments", treatments);
		root.put("ageranges", ageranges);
		root.put("disease", disease);
		DiseaseService service = new DiseaseService();
		List<Agerange> disease_ageranges = service.getAgerange(disease.getId());
		List<Place> disease_places = service.getPlace(disease.getId());
		List<Department> disease_departments = service.getDepartment(disease.getId());
		List<Treatment> disease_treatments = service.getTreatment(disease.getId());
		root.put("disease_ageranges", disease_ageranges);
		root.put("disease_places", disease_places);
		root.put("disease_departments", disease_departments);
		root.put("disease_treatments", disease_treatments);
		root.put("dtype", "disease");
		printFreemarker("my/disease_update.ftl", root);
	}
	/**
	 * 显示疾病详情
	 */
	public void show() {
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Disease disease=new Disease().get(id);
		if(disease==null){run_404();return;}
		DiseaseService service = new DiseaseService();
		List<Agerange> disease_ageranges = service.getAgerange(disease.getId());
		List<Place> disease_places = service.getPlace(disease.getId());
		List<Department> disease_departments = service.getDepartment(disease.getId());
		List<Treatment> disease_treatments = service.getTreatment(disease.getId());
		root.put("disease_ageranges", disease_ageranges);
		root.put("disease_places", disease_places);
		root.put("disease_departments", disease_departments);
		root.put("disease_treatments", disease_treatments);
		root.put("disease", disease);
		root.put("dtype", "disease");
		printFreemarker("my/disease_show.ftl", root);
		
	}
	/**
	 * 身体位置
	 */
	public void place() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), Place.class);
		root.put("page", page);
		root.put("dtype", "place");
		printFreemarker("my/disease_place.ftl", root);
	}
	
	/**
	 * 添加身体位置
	 */
	public void placeadd() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		printFreemarker("my/disease_placeadd.ftl", root);
	}
	
	/**
	 * 更新身体位置
	 */
	public void placeupdate() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		String[] params = request.getParams();
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Place place=new Place().get(id);
		if(place==null){run_404();return;}
		root.put("place", place);
		printFreemarker("my/disease_placeupdate.ftl", root);
	}
	
	/**
	 * 治疗方式
	 */
	public void treatment() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), Treatment.class);
		root.put("page", page);
		root.put("dtype", "treatment");
		printFreemarker("my/disease_treatment.ftl", root);
	}
	/**
	 * 添加治疗方式
	 */
	public void treatmentadd() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		printFreemarker("my/disease_treatmentadd.ftl", root);
	}
	
	/**
	 * 更新治疗方式
	 */
	public void treatmentupdate() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Treatment treatment=new Treatment().get(id);
		if(treatment==null){run_404();return;}
		root.put("treatment", treatment);
		printFreemarker("my/disease_treatmentupdate.ftl", root);
	}
	/**
	 * 科室
	 */
	public void department() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), Department.class);
		root.put("page", page);
		root.put("dtype", "department");
		printFreemarker("my/disease_department.ftl", root);
	}
	/**
	 * 添加治疗科室
	 */
	public void departmentadd() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		printFreemarker("my/disease_departmentadd.ftl", root);
	}
	
	/**
	 * 更新治疗科室
	 */
	public void departmentupdate() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Department department=new Department().get(id);
		if(department==null){run_404();return;}
		root.put("department", department);
		printFreemarker("my/disease_departmentupdate.ftl", root);
	}
	
	/**
	 * 年龄阶段
	 */
	public void agerange() {
		if(user.getRole()!=1&&user.getRole()==2&&user.getRole()==3)
		{
			run_500();return;
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), Agerange.class);
		root.put("page", page);
		root.put("dtype", "agerange");
		printFreemarker("my/disease_agerange.ftl", root);
	}
	/**
	 * 添加年龄阶段
	 */
	public void agerangeadd() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		printFreemarker("my/disease_agerangeadd.ftl", root);
	}
	
	/**
	 * 更新年龄阶段
	 */
	public void agerangeupdate() {
		if(user.getRole()!=1&&user.getRole()==2)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Agerange agerange=new Agerange().get(id);
		if(agerange==null){run_404();return;}
		root.put("agerange", agerange);
		printFreemarker("my/disease_agerangeupdate.ftl", root);
	}
}
